﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.BLL;
using AD.Model;

public partial class Manager_User_PageData_User : WebManagerForm
{
    public UserMod Custom =  new UserMod(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        if (!string.IsNullOrEmpty(action))
        {
            string idstr = Request.Params["id"];
            if (action.Equals("edit"))
            {
                UserMod user = null;
                if (!Page.IsPostBack)
                    user = UserBLL.QueryById(idstr);
                else
                {
                    user = UserBLL.QueryById(idstr);
                    if (user != null)
                    {
                        user.Email = Request["Email"];
                        user.Passwd = Request["Passwd"];
                        user.CompanyName = Request["CompanyName"];
                        user.MobilePhone = Request["MobilePhone"];
                        user.NetShopAddress = Request["NetShopAddress"];
                        user.NetShopName = Request["NetShopName"];
                        user.Product = Request["Product"];
                        user.QQ = Request["QQ"];
                        user.Recommended = Request["Recommended"];
                        user.TelPhone = Request["TelPhone"];
                        user.UnderstandChannel = Request["UnderstandChannel"];
                        user.Url = Request["Url"];
                        user.WangWang = Request["WangWang"];

                        int b;
                        int.TryParse(Request["BalancePoint"],out b);
                        if (user.BalancePoint != b)
                        {
                            int differ = b - user.BalancePoint;
                            int tmp = user.BalancePoint;
                            string tradeNo = "ad_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                            //调整
                            PayTradeMod pay = new PayTradeMod()
                                {
                                    Out_Trade_No = tradeNo,
                                    RewardType = PayTradeType.PointsAdjust,
                                    UserId = user.AutoID,
                                    RequestIp = Request.UserHostAddress,
                                    RewardPoints = differ,
                                    Commit_Date = DateTime.Now,
                                    InviteId = "",
                                    Balance = b,
                                    Status = false,
                                    IsInMoney = true,
                                };
                            if (PayTradeBLL.Insert(pay) > 0)
                            {
                                if (PayTradeBLL.UpdateInMoneyStatus(tradeNo) > 0 && PayTradeBLL.UpdatePaySuccessStatus(tradeNo) > 0)
                                {
                                    pay = PayTradeBLL.QueryByTradeNo(tradeNo);
                                    if (pay != null && pay.IsInMoney && pay.Status)
                                    {
                                        int effect = UtilHepler.UpdateAccountFlow(user, UtilHepler.GetMangerClientCookies(Request, Response).UserName, pay.RewardPoints, OperationType.PointsAdjust);
                                        if (effect <= 0)
                                        {
                                            PayTradeBLL.DeleteById(tradeNo);Alert("修改客户积分失败");
                                        }
                                    }
                                    else { PayTradeBLL.DeleteById(tradeNo); Alert("修改客户积分失败"); }
                                }
                                else { PayTradeBLL.DeleteById(tradeNo); Alert("修改客户积分失败"); }
                            }
                            else { PayTradeBLL.DeleteById(tradeNo); Alert("修改客户积分失败"); }
                        }
                        if (!string.IsNullOrEmpty(Request["Passwd"])) {
                            UserBLL.UpdatePasswd(user);
                        }
                        if (UserBLL.UpdateOtherInfo(user) > 0)
                        {
                            AlertRedirectStartup("修改客户信息成功", "UserList.aspx?action=gopage&pageIndex=1");
                        }
                        else {
                            Alert("修改客户信息失败");
                        }
                    }
                }
                if (user != null) Custom = user;
            }
            else if (action.Equals("add")) { 
            
            }
        }
    }
}